package andev.axplorer.Tiles;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.util.Map;

import andev.aandroidlib.view.MarqueeTextView;
import andev.axplorer.MainActivity;
import andev.axplorer.Menu.RightMenu;
import andev.axplorer.R;

public class TilesLayout extends LinearLayout {
    public static TilesLayout checkedTile;
    public MainActivity mainActivity;
    int resID = 0;
    Bitmap bm;
    private GestureDetector gestureDetector;
    private ImageView iv_icon;
    private MarqueeTextView tv_file_name;
    private Map<String, Object> TileMap;
    private String filePath;
    private String fileName;

    public TilesLayout(Context context) {
        super(context);
        mainActivity = (MainActivity) context;
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.tiles_layout, this, true);

        iv_icon = (ImageView) findViewById(R.id.iv_icon);

        tv_file_name = (MarqueeTextView) findViewById(R.id.tv_file_name);

        gestureDetector = new GestureDetector(context,
                new GestureDetector.SimpleOnGestureListener() {
                    @Override
                    public boolean onDoubleTap(MotionEvent e) {
                        if (checkedTile != null) {
                            setUnChecked();
                        }
                        goInDir();
                        return super.onDoubleTap(e);
                    }

                    @Override
                    public boolean onSingleTapUp(MotionEvent e) {
                        setChecked();
                        return super.onSingleTapUp(e);
                    }

                    @Override
                    public boolean onDown(MotionEvent e) {
                        mainActivity.hideKeyboard(true);
                        RightMenu.cancelRightMenu();
                        return true;
                    }

                    @Override
                    public void onLongPress(MotionEvent e) {
                        RightMenu.showRightMenu(TilesLayout.this, (int) e.getX(), (int) e.getY());
                        super.onLongPress(e);
                    }
                });
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            performClick();
        }
        return gestureDetector.onTouchEvent(event);
    }

    public void setUnChecked() {
        checkedTile.setBackgroundColor(Color.TRANSPARENT);
        tv_file_name.stopMarquee();
        checkedTile = null;

        mainActivity.updateDetailLine();
    }

    protected void setChecked() {
        if (checkedTile != null) {
            setUnChecked();
        }
        checkedTile = this;
        setBackgroundColor(Color.parseColor("#500000ff"));

        tv_file_name.startMarquee(true);

        mainActivity.updateDetailLine();
    }

    public void goInDir() {
        File file = getFile();
        if (file.isFile()) {
            mainActivity.openFile(file);
            return;
        }
        File[] files = file.listFiles();
        if (files == null) {
            Toast.makeText(mainActivity, "无法打开文件，需要ROOT权限", Toast.LENGTH_SHORT).show();
            return;
        }

        mainActivity.showFiles(files);
        mainActivity.tryDeletePathsBeforeAdd();

        MainActivity.addPathNow(filePath);
    }

    public void setFilePath(String path) {
        filePath = path;
    }

    public File getFile() {
        return new File(filePath);
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String name) {
        fileName = name;
        TextView tv_file_name = (TextView) findViewById(R.id.tv_file_name);
        tv_file_name.setText(name);
    }

    public void setFileIcon(int resId) {
        this.resID = resId;
        iv_icon.setImageResource(resId);
    }

    public Bitmap getFileIcon() {
        if (resID != 0) {
            return BitmapFactory.decodeResource(getResources(), resID);
        } else {
            return bm;
        }
    }

    public void setFileIcon(Bitmap bm) {
        this.bm = bm;
        iv_icon.setImageBitmap(bm);
    }

    public Map<String, Object> getTileMap() {
        return TileMap;
    }

    public void setTileMap(Map<String, Object> tileMap) {
        TileMap = tileMap;
    }
}
